﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.MENU.Components;
using System.Web.UI.HtmlControls;

namespace ASPNETVN.PORTAL.Modules.MENU.Pages
{
    public partial class Mootools : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Guid moduleID = new Guid(Request.QueryString["mid"]);

            CreateScript(moduleID);
        }

        private void CreateScript(Guid guidID)
        {
            //JQuery: jquery-1.3.2.js
            string scriptUrl = ResolveClientUrl("~/Scripts/JQuery/jquery-1.3.2.js");
            string scriptName = "jquery";
            Type scriptType = this.GetType();
            ClientScriptManager csm = Page.ClientScript;
            if (!csm.IsClientScriptIncludeRegistered(scriptType, scriptName))
            {
                csm.RegisterClientScriptInclude(scriptType, scriptName, scriptUrl);
            }

            scriptUrl = ResolveClientUrl("~/Modules/ASPNETVN.PORTAL.Modules.MENU/Scripts/mootools.js");
            scriptName = "mootools";
            if (!csm.IsClientScriptIncludeRegistered(scriptType, scriptName))
            {
                csm.RegisterClientScriptInclude(scriptType, scriptName, scriptUrl);
            }

            // Left Pane.
            LiteralControl literal = new LiteralControl("");
            literal.EnableViewState = true;
            LeftPane.EnableViewState = true;

            //BEGIN Tag
            literal.Text += "<div id=\"container\">";
            literal.Text += "<div id=\"menu\">";
            literal.Text += "<ul>";

            //GET DATA
            IList<Item> allItems = Item.SelectCollectionAll(guidID);
            IList<Item> topItems = allItems.Where(x => x.ParentID == 0).OrderBy(x => x.SortOrder).ToList();

            foreach (Item topItem in topItems)
            {
                //Get child menu 1
                IList<Item> childItems1 = allItems.Where(x => x.ParentID == topItem.ID).OrderBy(x => x.SortOrder).ToList();

                if (childItems1.Count > 0)
                    literal.Text += string.Format(
                        "<li><a style=\"background: url('../images/MootoolMenu/bg.jpg') repeat scroll 0% 0% transparent;" +
                        "margin-left: 0px;\" href=\"#\" title=\"\" class=\"toggler\">{0}</a></li>", topItem.Title);
                else
                    literal.Text += string.Format(
                        "<li><a style=\"background: url('../images/MootoolMenu/bg.jpg') repeat scroll 0% 0% transparent;" +
                        "margin-left: 0px;\" href=\"{1}\" title=\"{0}\" target=\"{2}\" class=\"toggler\">{0}</a></li>", topItem.Title, Global.GetApplicationPath() + "/" + topItem.Url, topItem.Target);

                //Begin Tag
                literal.Text += string.Format(
                    "<div style=\"padding-top: 0px; border-top: medium none;" +
                    " padding-bottom: 0px; border-bottom: medium none;" +
                    " overflow: hidden; visibility: hidden; opacity: 0; height: 0px;\"" +
                    " class=\"accordion\">");


                foreach (Item item1 in childItems1)
                {
                    //Get child menu 2
                    IList<Item> childItems2 = childItems1.Where(x => x.ParentID == item1.ID).OrderBy(x => x.SortOrder).ToList();

                    if (childItems2.Count > 0)
                        literal.Text += string.Format("<a class=\"toggler2\" href=\"#\" title=\"{0}\">{0}</a>", item1.Title);
                    else
                        literal.Text += string.Format("<a href=\"{0}\" title=\"{1}\" target=\"{2}\">{1}</a>", item1.Url, item1.Title, item1.Target);

                    //Begin Tag
                    literal.Text += string.Format(
                        "<div style=\"padding-top: 0px; border-top: medium none;" +
                        " padding-bottom: 0px; border-bottom: medium none;" +
                        " overflow: hidden; visibility: hidden; opacity: 0; height: 0px;\"" +
                        " class=\"accordion2\">");

                    foreach (Item item2 in childItems2)
                    {
                        literal.Text += "<ul>";
                        literal.Text += string.Format("<li><a href=\"{0}\" title=\"{1}\" target=\"{2}\">{1}</a></li>", item2.Url, item2.Title, item2.Target);
                        literal.Text += "</ul>";
                    }

                    //End Tag
                    literal.Text += "</div>";
                }

                //End Tag
                literal.Text += "</div>";
            }
            //END Tag
            literal.Text += "</ul></div></div>";
            LeftPane.Controls.Add(literal);


            HtmlLink cssLink = new HtmlLink();
            cssLink.Href = "~/Modules/ASPNETVN.PORTAL.Modules.MENU/StyleSheets/MooToolMenu.css";
            cssLink.Attributes.Add("rel", "stylesheet");
            cssLink.Attributes.Add("type", "text/css");
            Page.Header.Controls.Add(cssLink);
        }
    }
}
